package com.plan.controller;

import com.plan.entity.School;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.plan.entity.User;
import com.plan.service.UserService;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    /*
登录
*/
    @PostMapping("login")
    public Map<String, Object> login(@RequestBody User user){
        log.info("当前登录的用户信息："+user.toString());

        Map<String, Object>map = new HashMap<>();
        try{
            User userDB =userService.login(user);
            userDB.setUsername("");
            userDB.setPassword("");
            if(userDB.getStatus().equals("1")){
                map.put("state",true);
                map.put("msg","用户登录成功");
                map.put("user",userDB);
                System.out.println(userDB);
            }else{
                map.put("state",false);
                map.put("msg","已注销");
            }

        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","输入错误，请重试");
        }
        return map;
    }

    @GetMapping("findUser")
    public  User findUser(String id){
        log.info("查询user信息的id:[{}]"+id);
        User userDB=userService.findUser(id);
         if(userDB.getSex().equals("1")){
             userDB.setSex("男");
         }else {
             userDB.setSex("女");
         }
         return userDB;
    }
    @GetMapping("search")
    public Map<String,Object>search(String word,String word2,String word3)  {
        if(word==null){
            word="";
        } if(word2==null){
            word2="";
        } if(word3==null){
            word3="";
        }
        log.info(word+word2+word3);
        HashMap<String,Object> map=new HashMap<>();
            List<School> schools= userService.search(word,word2,word3);
            map.put("schools", schools);
        return map;
    }
}
